#include <dbg.h>

#include <climits>
#include <numeric>

using namespace std;

class Solution {
public:
    int minSizeSubarray(vector<int>& nums, int target) {
        int n = nums.size();
        int res = INT_MAX;
        long long sum = 0;
        long long total = accumulate(nums.begin(), nums.end(), 0LL);
        int left = 0;
        for (int right = 0; right < 2 * n; right++) {
            sum += nums[right % n];
            while (sum > target % total) {
                sum -= nums[left % n];
                ++left;
            }
            if (sum == target % total) {
                res = min(res, right - left + 1);
            }
        }
        return res == INT_MAX ? -1 : res + target / total * n;
    }
};

int main() {
    Solution s;

    vector<int> nums1 = {1, 2, 3};
    vector<int> nums2 = {1, 1, 1, 2, 3};
    vector<int> nums3 = {2, 4, 6, 8};
    vector<int> nums4 = {1, 2, 2, 2, 1, 2, 1, 2, 1, 2, 1};

    int target1 = 5;
    int target2 = 4;
    int target3 = 3;
    int target4 = 83;

    dbg(s.minSizeSubarray(nums1, target1));  // 2
    dbg(s.minSizeSubarray(nums2, target2));  // 2
    dbg(s.minSizeSubarray(nums3, target3));  // -1
    dbg(s.minSizeSubarray(nums4, target4));  // 53

    return 0;
}